<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateProductKey{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateProduct = new Zend_Validate();
        $validateKey = new Zend_Validate();
        
        $option = array('table' => 'product_keys', 'field' => 'key');
        
        $validateProduct->addValidator(new Zend_Validate_GreaterThan(0), true);
        
        $validateKey->addValidator(new Zend_Validate_NotEmpty(), true)
                    ->addValidator(new Zend_Validate_Db_NoRecordExists($option), true);
        
        /*
        * Check Product
        */
        
        if(!$validateProduct->isValid($params['product'])){
            $this->_messageError['product'] = '<label class="control-label" for="inputError">' . $translate->_('error product key') . '</label>';
            $this->_arrData['product'] = '';
            $this->_classError['product'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['product'] = $data['product'];
            }
        }else{
            $this->_messageError['product'] = '';
            $this->_arrData['product'] = $params['product'];
            $this->_classError['product'] = '';
        }
        
        /*
        * Check Key
        */
        
        if(!$validateKey->isValid($params['key'])){
            $this->_messageError['key'] = '<label class="control-label" for="inputError">' . $translate->_('key') . current($validateKey->getMessages()) . '</label>';
            $this->_arrData['key'] = '';
            $this->_classError['key'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['key'] = $data['key'];
            }
        }else{
            $this->_messageError['key'] = '';
            $this->_arrData['key'] = $params['key'];
            $this->_classError['key'] = '';
        }
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        $info = new Zenddn_System_Info();
        $user_id = $info->getUserInfo('id');
        
        if(!$edit){
            $this->_arrData['created'] = $curDate;
            $this->_arrData['created_by'] = $user_id;
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}